# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=gimp
_pkgver=3.0.4
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=${_pkgver}
pkgrel=1
pkgdesc="GNU Image Manipulation Program (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.gimp.org/downloads/devel/"
msys2_repository_url="https://gitlab.gnome.org/GNOME/gimp"
msys2_references=(
  'archlinux: gimp'
  'aur: gimp-devel'
  "cpe: cpe:/a:gimp:gimp"
)
license=('spdx:GPL-3.0-or-later AND LGPL-3.0-or-later')
provides=("${MINGW_PACKAGE_PREFIX}-gimp3")
conflicts=(
  "${MINGW_PACKAGE_PREFIX}-gimp3"
  "${MINGW_PACKAGE_PREFIX}-gimp2"
)
replaces=("${MINGW_PACKAGE_PREFIX}-gimp3")
depends=("${MINGW_PACKAGE_PREFIX}-appstream-glib"
         #"${MINGW_PACKAGE_PREFIX}-aalib"
         "${MINGW_PACKAGE_PREFIX}-atk"
         "${MINGW_PACKAGE_PREFIX}-babl"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-cfitsio"
         "${MINGW_PACKAGE_PREFIX}-drmingw"
         "${MINGW_PACKAGE_PREFIX}-exiv2"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
         "${MINGW_PACKAGE_PREFIX}-gegl"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-gexiv2"
         "${MINGW_PACKAGE_PREFIX}-ghostscript"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-glib-networking"
         "${MINGW_PACKAGE_PREFIX}-gtk3"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-iso-codes"
         "${MINGW_PACKAGE_PREFIX}-json-glib"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libarchive"
         "${MINGW_PACKAGE_PREFIX}-libheif"
         "${MINGW_PACKAGE_PREFIX}-libiff"
         "${MINGW_PACKAGE_PREFIX}-libilbm"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libjxl"
         "${MINGW_PACKAGE_PREFIX}-libmng"
         "${MINGW_PACKAGE_PREFIX}-libmypaint"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-librsvg"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libwmf"
         #"${MINGW_PACKAGE_PREFIX}-luajit"
         #"${MINGW_PACKAGE_PREFIX}-lua51-lgi"
         "${MINGW_PACKAGE_PREFIX}-mypaint-brushes"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-pango"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-gobject"
         "${MINGW_PACKAGE_PREFIX}-shared-mime-info"
         "${MINGW_PACKAGE_PREFIX}-xpm-nox"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools"
             "${MINGW_PACKAGE_PREFIX}-gi-docgen"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-libxslt"
             "${MINGW_PACKAGE_PREFIX}-qoi"
             "${MINGW_PACKAGE_PREFIX}-vala")
source=("https://download.gimp.org/pub/gimp/v3.0/gimp-${_pkgver}.tar.xz"
        0001-disable-codeview.patch
        0002-dont-ignore-destdir.patch)
sha256sums=('8caa2ec275bf09326575654ac276afc083f8491e7cca45d19cf29e696aecab25'
            '0bd52e8379f76202742b687f3f3e4eb498637a0a260e1e790757a668a5da48eb'
            '5535197b3f8526d89e4b06dcfb9b31aad59531f9a2c9cfbafbafbdb47460b279')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "gimp-${_pkgver}"
  # https://gitlab.gnome.org/GNOME/gimp/-/merge_requests/2275
  apply_patch_with_msg \
    0001-disable-codeview.patch \
    0002-dont-ignore-destdir.patch
}

build() {
  CFLAGS+=" -Wno-incompatible-pointer-types" \
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  meson setup \
    --buildtype=plain \
    --auto-features=enabled \
    --prefix=${MINGW_PREFIX} \
    -Daa=disabled \
    -Dalsa=disabled \
    -Dappdata-test=disabled \
    -Dbug-report-url="https://github.com/msys2/MINGW-packages/issues" \
    -Dcheck-update=no \
    -Ddirectx-sdk-dir="${MINGW_PREFIX}" \
    -Dgi-docgen=disabled \
    -Dgudev=disabled \
    -Dheadless-tests=disabled \
    -Djavascript=disabled \
    -Dlibbacktrace=false \
    -Dlibunwind=false \
    -Dlinux-input=disabled \
    -Dxcursor=disabled \
    -Dwin32-debug-console=disabled \
    "build-${MSYSTEM}" \
    "gimp-${_pkgver}"

  meson compile -C "build-${MSYSTEM}"
}

package() {
  meson install -C "build-${MSYSTEM}" --destdir="${pkgdir}"

  # Adjust the python interpreter path
  sed -e "s|\=.*|\=python3w.exe|g" -i "${pkgdir}${MINGW_PREFIX}"/lib/gimp/3.0/interpreters/pygimp.interp
  sed -e "s|::python3|::python3w.exe|g" -i "${pkgdir}${MINGW_PREFIX}"/lib/gimp/3.0/interpreters/pygimp.interp

  rm -f "${pkgdir}${MINGW_PREFIX}/lib/gimp/3.0/modules/*.dll.a"

  install -Dm644 "gimp-${_pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm644 "gimp-${_pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
